<template>
  <div class="app-container">
    <div class="content-card password-card">
      <div class="card-header">
        <h2>修改密码</h2>
      </div>
      <el-form
          ref="form"
          :model="form"
          status-icon
          :rules="rules"
          label-width="100px"
          class="password-form"
      >
        <el-form-item label="原密码" prop="password2">
          <el-input
              v-model="form.password2"
              type="password"
              autocomplete="off"
              show-password
              placeholder="请输入原密码"
          >
            <template #prefix><el-icon class="el-input__icon"><Lock /></el-icon></template>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input
              v-model="form2.password"
              type="password"
              autocomplete="off"
              show-password
              placeholder="请输入新密码"
          >
            <template #prefix><el-icon class="el-input__icon"><Key /></el-icon></template>
          </el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkpassword">
          <el-input
              v-model="form.checkpassword"
              type="password"
              autocomplete="off"
              show-password
              placeholder="请再次输入新密码"
          >
            <template #prefix><el-icon class="el-input__icon"><Key /></el-icon></template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" style="width: 120px">
            <el-icon><Check /></el-icon>提交
          </el-button>
          <el-button @click="resetForm('form')" style="width: 120px">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "../utils/request";
import { ElMessage } from "element-plus";
import { Lock, Key, Check, Refresh } from '@element-plus/icons-vue'

export default {
  name: "Password",
  components: {
    Lock, Key, Check, Refresh
  },
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入原密码'))
      } else {
        if (this.form.password2 !== this.form.truepassword) {
          callback(new Error('原密码错误'))
        }
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        callback()
      }
    }
    const validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form2.password) {
        callback(new Error("两次输入密码不匹配"))
      } else {
        callback()
      }
    }
    return {
      form: {
        password2: '',
        checkpassword: '',
        truepassword:'',
      },
      form2:{
        password:'',
        id:0
      },
      rules: {
        password: [{ validator: validatePass, trigger: 'blur' ,required: true}],
        checkpassword: [{ validator: validatePass3, trigger: 'blur',required: true,}],
        password2: [{ validator: validatePass2, trigger: 'blur',required: true,}],
      },
    }
  },
  created() {
    let user = JSON.parse(sessionStorage.getItem("user"))
    this.form.truepassword = user.password
    this.form2.id = user.id

  },
  methods: {
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.put("/user", this.form2).then(res => {
            if (res.code == 0) {
              ElMessage.success("密码修改成功,请重新登录")
              sessionStorage.removeItem("user")//清空缓存的用户信息
              this.$router.push("/login")//跳转登录界面
            } else {
              ElMessage.error(res.msg)
            }
          })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style scoped>
.app-container {
  padding: 20px;
  background-color: var(--color-bg-1);
  min-height: calc(100vh - 60px);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.content-card {
  background: #fff;
  border-radius: 4px;
  padding: 40px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  width: 100%;
  max-width: 600px;
  margin-top: 40px;
}

.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.card-header h2 {
  margin: 0;
  color: var(--color-text-1);
  font-weight: 600;
}

.password-form {
  max-width: 450px;
  margin: 0 auto;
}
</style>
